using UnityEngine;
using System.Collections;

public class Sprite
{
	protected Vector2 tiling = new Vector2();//texture tiling
	protected Vector2 offset = new Vector2();//texture offset value
	protected Vector2 frames = new Vector2();//number of frames horizontally and vertically
	protected Vector2 currentFrame = new Vector2();//current frame of the animation
	protected Transform target;
	
	int count = 0;
	int fps = 30;
	
	public Sprite(Transform target,Vector2 frames, Vector2 startingFrame)
	{
		this.target = target;
		this.frames = frames;
		tiling = new Vector2((1.0f/frames.x),(1.0f/frames.y));
		target.renderer.material.SetTextureScale("_MainTex", tiling);
		offset = new Vector2((1.0f/frames.x)*startingFrame.x,(1.0f/frames.y)*startingFrame.y);
		target.renderer.material.SetTextureOffset("_MainTex", offset);
		currentFrame = startingFrame;
	}
	// Set current frame of the animation
	public void setFrame(Vector2 offsetFrame)
	{
		offset = new Vector2((1.0f/frames.x)*offsetFrame.x,(1.0f/frames.y)*offsetFrame.y);
		target.renderer.material.SetTextureOffset("_MainTex", offset);
	}
	public void Animate()
	{
		count++;
		if(count % fps == 0)
		{
			if(currentFrame.y+1 <= frames.y)
			{
				if(currentFrame.x < frames.x)
				{
					setFrame(currentFrame);
					currentFrame.x++;
				}
				else
				{
					currentFrame.x = 0;
					currentFrame.y++;
				}
			}
			else
			{
				currentFrame = new Vector2(0,0);
			}
		}
		
	}
	public void Animate(int frameY)
	{
		count++;
		if(count % fps == 0)
		{
			currentFrame.y = frameY;
			if(currentFrame.x < frames.x)
			{
				setFrame(currentFrame);
				currentFrame.x++;
			}
			else
			{
				currentFrame.x = 0;
			}
		}
		
	}
	// Update is called once per frame
	void Update () 
	{
	
	}
}
